<?php
	class Admin_NewsController extends My_Controller_Action{
		private $_model = '';
		private $_moduleName = '';
		private $_controllerName = '';
		private $_actionName = '';
		private $_actionMain = '';
		private $_params = array ();
		private $_userId = '';
		
		public function init(){ 
			$template_path = TEMPLATE_PATH . "/admin";
			$this->loadTemplate($template_path,'template.ini','template');
			$this->_model = new Admin_Model_News();
			
			$this->_moduleName = $this->_request->getModuleName ();
			$this->_controllerName = $this->_request->getControllerName ();
			$this->_actionName = $this->_request->getActionName ();
			$this->_params = $this->_request->getParams ();
			$this->_actionMain = '/' . $this->_moduleName . '/' . $this->_controllerName . '/index';
			$this->view->controllerName = $this->_controllerName;
			
			$auth = Zend_Auth::getInstance ();
			$infoUser = $auth->getIdentity ();
			$this->_userId = $infoUser ['userId'];
			
			
			
			$this->view->headScript ()
			->appendFile ( BASE_URL . '/public/templates/admin/js/ckeditor/ckeditor.js' );
			
			My_Auth_User::authenticate();
		}
		public function indexAction(){
			$this->_redirect ( "/{$this->_moduleName}/{$this->_controllerName}/show-all" );
		}
		public function showAllAction(){
			$news = $this->_model->getAll();
			$this->view->news = $news;
		}
		public function addAction(){ 
			$data = $this->_request->getPost();
			$form = new Admin_Form_News();
			
			if($this->_request->isPost()){ 
				$valid = new Admin_Form_Validators_NewsValid($data);
				if( $form->isValid ( $data ) && ($valid->isValid () == TRUE)){
					$data = $form->getValues();

						$upload = new My_File_Upload();
						$data['image'] = $upload->upload('image','news', array('task'=>'rename','isThumb' => TRUE));
						//$data = $this->_request->getPost();
						$product = $this->_model->addItem($data);
				}
			}
		if ($product) {
					if (isset ( $this->_params ['btnSave'] )) {
						$this->view->popupMessage = $this->_helper->popupMessage ( MESS_ADD, $this->_actionMain );
					} elseif (isset ( $this->_params ['btnSaveAdd'] )) {
						$this->_redirect ( "/{$this->_moduleName}/{$this->_controllerName}/{$this->_actionName}" );
					}
				}
			 else {
				$this->view->error = $valid->messages;
			}
			$this->view->form = $form;
		
		}
	}
